package org.niu.array;

import java.util.HashMap;
import java.util.Map;

public class Bank {
    Map<Integer,Long> mp = new HashMap<>();

    public Bank(long[] balance) {
        for (int i = 0; i < balance.length; i++) {
            mp.put((i+1),balance[i]);
        }
    }

    public boolean transfer(int account1, int account2, long money) {
        if(!mp.containsKey(account1)||!mp.containsKey(account2)){
            return false;
        }
        long m1 = mp.get(account1);
        long m2 = mp.get(account2);
        if(m1 < money){
            return false;
        }else{
            mp.put(account1,m1-money);
            mp.put(account2,m2+money);
            return true;
        }
    }

    public boolean deposit(int account, long money) {
        if(!mp.containsKey(account)){
            return false;
        }
        mp.put(account,mp.get(account)+money);
        return true;
    }

    public boolean withdraw(int account, long money) {

        if(!mp.containsKey(account)){
            return false;
        }
        Long m1 = mp.get(account);
        if(m1 < money){
            return false;
        }else{
            mp.put(account,m1-money);
            return true;
        }
    }
}
